/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.core.service;

import com.github.lvyanyang.annotation.OperateLog;
import com.github.lvyanyang.core.BaseService;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.PageList;
import com.github.lvyanyang.core.XCI;
import com.xci.edu.core.dao.EduQuestionFavoriteDao;
import com.xci.edu.core.entity.EduQuestionFavorite;
import com.xci.edu.core.filter.EduQuestionFavoriteFilter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 题库收藏服务
 * @author 马星俊 maxingjun@xci96716.com
 * @since 2020-12-24 14:51:30
 */
@Service
public class EduQuestionFavoriteService extends BaseService {
    /** 题库收藏数据层对象 */
    @Resource private EduQuestionFavoriteDao eduQuestionFavoriteDao;

    /**
     * 新建题库收藏
     * @param studentId 学员Id
     * @param questionId  题库Id
     * @param favoriteType 收藏类型
     * @return
     */
    @OperateLog(msg = "新建题库收藏", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult insert(Long studentId, Long questionId, Integer favoriteType) {
        EduQuestionFavorite entity = new EduQuestionFavorite();
        entity.setEduStudentId(studentId);
        entity.setEduQuestionId(questionId);
        entity.setFavoriteType(favoriteType);
        entity.setCreateDateTime(XCI.now());
        return save(entity, true);
    }

    /**
     * 新建错题收藏
     * @param studentId 学员Id
     * @param questionId  题库Id
     * @return
     */
    @OperateLog(msg = "新建题库收藏", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult insertError(Long studentId, Long questionId) {
        //先判断题库是否存在此错题
        if(!selectByIdCondition(studentId,questionId,1)){
            EduQuestionFavorite entity = new EduQuestionFavorite();
            entity.setEduStudentId(studentId);
            entity.setEduQuestionId(questionId);
            entity.setFavoriteType(1);
            entity.setCreateDateTime(XCI.now());
            return save(entity, true);
        }else{
            return BoolResult.success();
        }
    }

    /**
     * 修改题库收藏
     * @param entity 题库收藏实体
     */
    @OperateLog(msg = "修改题库收藏", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult update(EduQuestionFavorite entity) {
        return save(entity, false);
    }

    /**
     * 修改题库收藏状态
     * @param ids    题库收藏主键字符串
     * @param status 题库收藏状态
     */
    @OperateLog(msg = "修改题库收藏状态", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(@NotBlank(message = "请指定题库收藏主键字符串") String ids, @NotNull(message = "请指定题库收藏状态") Boolean status) {
        XCI.forEachIds(ids, id -> {
            eduQuestionFavoriteDao.updateStatus(id, status);
        });
    }

    /**
     * 删除题库收藏
     * @param ids 题库收藏主键字符串
     */
    @OperateLog(msg = "删除题库收藏", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult delete(@NotBlank(message = "请指定题库收藏主键字符串") String ids) {
        XCI.forEachIds(ids, id -> {
            var entity = eduQuestionFavoriteDao.selectById(id);
            if (entity == null) return;
            eduQuestionFavoriteDao.deleteById(entity.getId());
        });
        return BoolResult.success();
    }

    /**
     * 删除题库收藏
     * @param studentId 学员Id
     * @param questionId  题库Id
     * @param favoriteType 收藏类型
     * @return
     */
    @OperateLog(msg = "删除题库收藏", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult delete(Long studentId,Long questionId,int favoriteType) {
        eduQuestionFavoriteDao.deleteByIdCondition(studentId,questionId,favoriteType);
        return BoolResult.success();
    }

    /**
     * 清空题库收藏
     * @param studentId 学员Id
     * @param favoriteType 收藏类型
     * @return
     */
    @OperateLog(msg = "清空题库收藏", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult clear(Long studentId,int favoriteType) {
        eduQuestionFavoriteDao.deleteByIdCondition(studentId,null,favoriteType);
        return BoolResult.success();
    }

    /**
     * 根据条件查询单个试题是否收藏
     * @param studentId 学员Id
     * @param questionId  题库Id
     * @param favoriteType 收藏类型
     * @return
     */
    @OperateLog(msg = "查询试题收藏状态", module = "题库收藏")
    @Transactional(rollbackFor = Exception.class)
    public boolean selectByIdCondition(Long studentId,Long questionId,int favoriteType) {
        return eduQuestionFavoriteDao.selectByIdCondition(studentId,questionId,favoriteType);
    }

    /**
     * 根据主键查询单个题库收藏
     * @param id 题库收藏主键
     * @return 返回题库收藏实体
     */
    public EduQuestionFavorite selectById(@NotNull(message = "请指定题库收藏主键") Long id) {
        return eduQuestionFavoriteDao.selectById(id);
    }

    /**
     * 查询题库收藏列表
     * @param filter 过滤条件
     * @return 返回题库收藏列表
     */
    public List<EduQuestionFavorite> selectList(EduQuestionFavoriteFilter filter) {
        return eduQuestionFavoriteDao.selectList(filter);
    }

    /**
     * 查询题库收藏分页列表
     * @param filter 过滤条件
     * @return 返回题库收藏分页列表
     */
    public PageList<EduQuestionFavorite> selectPageList(EduQuestionFavoriteFilter filter) {
        return PageList.of(eduQuestionFavoriteDao.selectPageList(filter));
    }

    /**
     * 保存数据,在insert和update之前和之后的校验和处理
     * @param entity  题库收藏实体
     * @param created 是否新建
     */
    private BoolResult save(EduQuestionFavorite entity, boolean created) {
        //新建时如果主键未指定,自动设置主键
        XCI.ifTrueAction(XCI.invalidId(entity.getId(), created), () -> entity.setId(XCI.nextId()));
        //如果简拼为空,设置简拼
        //XCI.ifBlankAction(entity.getSpell(), () -> entity.setSpell(XCI.getSpell(entity.getName())));

        //检查题库收藏编码是否存在
        //if (eduQuestionFavoriteDao.existxByCode(entity.getCode(), XCI.getExcludeId(entity.get(), created))) {
        //    return BoolResult.fail(XCI.format("题库收藏编码[{}]已经存在", entity.getCode()));
        //}

        //更新数据库
        if (created) {
            eduQuestionFavoriteDao.insert(entity);
            //logService.saveInsertHistoryLogAsync(entity.get(), entity);
        } else {
            //var before = eduQuestionFavoriteDao.selectById(entity.get());
            eduQuestionFavoriteDao.update(entity);
            //logService.saveUpdateHistoryLogAsync(entity.get(), before, entity);
        }
        return BoolResult.success();
    }
}